package com.example.controller.orders;

import cn.hutool.core.lang.Dict;
import com.example.common.Result;
import com.example.entity.orders.OrderDetails;
import com.example.entity.orders.Orders;
import com.example.entity.orders.orderDTO.OrderDetailsDTO;
import com.example.entity.orders.orderDTO.OrderRequest;
import com.example.entity.shop.SellerProduct;
import com.example.service.order.OrderService;
import com.example.service.shop.SellerProductService;
import com.example.service.utils.ProductDescriptionMatcher;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单接口
 */
@RestController
@RequestMapping("/orders")
public class OrdersConrtoller {

    @Resource
    private SellerProductService sellerProductService;
    @Resource
    ProductDescriptionMatcher productDescriptionMatcher;
     @Resource
    private OrderService orderService;

    /**
     * 根据患者描述推荐药品
     *
     * @return
     */
    @PostMapping("/matchDescription")
    public Result findMatchingProducts(@RequestBody SellerProduct sellerProduct) {
        // 从数据库中读出所有的列表
        List<SellerProduct> products = sellerProductService.selectAll(sellerProduct);
        // 匹配
        List<SellerProduct>  productsDescription = productDescriptionMatcher.findMostSimilarProducts(sellerProduct.getDescription(), products);
        return Result.success(productsDescription);
    }

    /**
     * 创建订单
     * @param orderRequest
     * @return
     */
    @PostMapping("/createOrder")
    public Result createOrder(@RequestBody OrderRequest orderRequest) {
        Orders order = orderRequest.getOrder();
        List<OrderDetails> orderDetailsList = orderRequest.getOrderDetailsList();
        orderService.createOrder(order, orderDetailsList);
        return Result.success();
    }

    /**
     * 分页查询
     * @param orders
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/selectAllByPage")
    public Result selectAllByPage(Orders orders,
                                  @RequestParam(defaultValue = "1")Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Orders> page = orderService.selectAllByPage(orders, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 根据订单id查询订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/selectOrderDetailsByOrderId/{orderId}")
    public Result selectOrderDetailsByOrderId(@PathVariable Integer orderId) {
        OrderDetailsDTO orderDetailsDTO = orderService.selectOrderDetailsByOrderId(orderId);
        return Result.success(orderDetailsDTO);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping("/updateOrderStatus")
    public Result updateOrderStatus(@RequestBody Orders orders) {
        orderService.updateOrderStatus(orders);
        return Result.success();
    }

    /**
     * 审核退货申请
     */



    /**
     * 根据订单号查询订单
     * @param orderNumber
     * @return
     */
    @GetMapping("/selectOrderByOrderNum")
    public Result selectOrderByOrderNumber(String orderNumber) {
        Orders orders = orderService.selectOrderByOrderNumber(orderNumber);
        return Result.success(orders);
    }


    /**
     * 批量删除
     * @param orders
     * @return
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Orders> orders){
        orderService.deleteBatch(orders);
        return Result.success();
    }

    /**
     * 统计销售金额情况
     */
    @GetMapping("/countSales")
    public Result countSales(Integer days) {
        List<Dict> dictList = orderService.countBuyerPayout( days);
        return Result.success(dictList);
    }

    /**
     * 统计药物类别销量
     */
    @GetMapping("/countSalesByCategory")
    public Result countSalesByCategory(Orders order) {
        List<Dict> dictList = orderService.countCategorySales(order);
        return Result.success(dictList);
    }


}
